package demo;

import org.jsoup.select.Elements;

import app.client.file.downs.FileDown;
import app.netant.NetAntBase;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.file.FilePath;
import cn.lily.com.lang.number.MathUtil;
/**
 * 商品流通爬虫
 * @author JiaJ
 * @date 2018年6月19日
 *
 */
public class BcargoAnt extends NetAntBase {
	
	private DBManager dbManager;

	public BcargoAnt(String netSite) {
		super(netSite);
	}

	/**
	 * 查询类型
	 */
	public void searchTyp(){
		
		DBOfflineManager offline = new DBOfflineManager("bcargo_mysql");
		this.dbManager = offline.getManager();
		
		Elements activePart = document.select(".page-layout.page-main-content .main-wrap.J_Region "
				                            + "#guid-396102 .module-wrap.J_tb_lazyload .huichi-nav-container "
				                            + ".huichi-cats ul.items-menu-container.J_Nav li.items "
				                            + ".subitems-container ul.sub-cats-container li.sub_items");
		
		int typeIdx = 0;
		int proIdx = 0;
		
		for(int i=0;i<activePart.size();i++){
			
			try {
				
				Enviroment.getEnv().getLogBean().beginLog();
				
				String type = activePart.get(i).select(".title .text").text();
				String href = activePart.get(i).select(".title .pic").attr("src");		
				String typeID = "MT"+typeIdx;typeIdx++;
				
				if(href.startsWith("//")){
					href = "http:"+href;
				}
				
				String filePath = this.downImage(href);
				
				DataTable table = this.dbManager.executeQueryTable("select * from ${schema}.COC_PRODUCT_CLASSIFY where FS_CLASSIFYID='"+typeID+"'");
				if(table.next()){
					
				}else{
					this.dbManager.executeUpdate("insert into ${schema}.COC_PRODUCT_CLASSIFY (FS_CLASSIFYID,FS_PAR_CLASSIFYID,FS_CLASSIFYNAME) "
	                          + "values ('"+typeID+"','0','"+type+"') ");
				}		
				
				Elements childParts = activePart.get(i).select(".sub-lists.item-links span a");
				for(int j=0;j<childParts.size();j++){			
					
					try {
						
						String proID = "MP"+proIdx;proIdx++;
						
						DataTable proTable = this.dbManager.executeQueryTable("select * from ${schema}.COC_PRODUCT_PRODUCT where FS_PROD_ID='"+proID+"'");
						if(proTable.next()){
							continue;
						}
						
						String proHref = childParts.get(j).attr("href");
						String proSale = this.getSale(proHref);
						
						String proName = childParts.get(j).text();
						
						String proUnit = "件";
								
						System.out.println("开始处理商品："+proName);
						
						this.dbManager.executeUpdate("insert into ${schema}.COC_PRODUCT_PRODUCT "
								                   + "(FS_PROD_ID,FS_CLASSIFY_ID,FS_PROD_NAME,FS_UNIT,FS_IMG_URL,FS_MEMO,FI_ADV_UNITPRICE) "
								                   + "values "
								                   + "('"+proID+"','"+typeID+"','"+proName+"','"+proUnit+"','"+filePath+"','',"+proSale+")");
						
						
						this.dbManager.executeUpdate("insert into ${schema}.COC_JOBBER_SHOP "
								                   + "(FS_JOBBER_ID,FS_PROD_ID,FS_PROD_NAME,FI_ADV_PROFIT_PERC,"
								                   + "FI_PROD_COST_CREAT,FS_UNIT,FI_RROD_STATE,FS_CLASSIFYID,FS_CLASSIFYNAME,FS_IMG_URL) "
								                   + "values "
								                   + "('00000000','"+proID+"','"+proName+"',1,"+proSale+",'件','00','"+typeID+"','"+type+"','"+filePath+"')");

						
					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}
					
				}
				
				Enviroment.getEnv().getLogBean().endLog();
				
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}

		}
		
		
	}
	
	private String getSale(String proHref){		
		
		System.out.println("开始连接地址："+proHref);
		
		BcargoDetailAnt ant = new BcargoDetailAnt(proHref);	
		return ant.getFirstSale();		
	}
	
	private String downImage(String imgHref){
		
		System.out.println("开始下载图片"+imgHref);
		
		int endPoint = imgHref.lastIndexOf("/");		
		String fileName = imgHref.substring(endPoint+1);
		String fileExp = fileName.substring(fileName.lastIndexOf(".")+1);	
		String retPath = "/file/coc/pros/MC"+System.currentTimeMillis()+""+MathUtil.random(3)+"."+fileExp;
		String filePath = FilePath.getWebRootPath(retPath);
		
		try {
			FileDown fileDown = new FileDown();
			fileDown.downImage(imgHref, filePath);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
		return retPath;
		
	}
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		BcargoAnt ant = new BcargoAnt("https://chi.taobao.com/?spm=a21bo.2017.201867-main.25.5af911d9Zue2Jm");
		ant.searchTyp();
	}
	
}
